package com.scoutmobmap.client;

import java.util.List;

import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.scoutmobmap.shared.OfferDto;

public class ListPanel extends ScrollPanel {
  public void initializeData(List<OfferDto> offers) {
    ProvidesKey<OfferDto> keyProvider = new ProvidesKey<OfferDto>() {
      @Override public Object getKey(OfferDto item) {
        return (item == null) ? null : item.details;
      }
    };
    
    OfferCell offerCell = new OfferCell();
    CellList<OfferDto> cellList = new CellList<OfferDto>(offerCell, keyProvider);
    ListDataProvider<OfferDto> dataProvider = new ListDataProvider<OfferDto>();
    dataProvider.addDataDisplay(cellList);
    
    cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
    final SingleSelectionModel<OfferDto> selectionModel = new SingleSelectionModel<OfferDto>(keyProvider);
    cellList.setSelectionModel(selectionModel);
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      public void onSelectionChange(SelectionChangeEvent event) {
        OfferDto selected = selectionModel.getSelectedObject();
        if (selected != null) {
//          Window.alert("You selected: " + selected.expiration);
        }
      }
    });
    
    
    cellList.setRowCount(offers.size(), true);
    cellList.setPageSize(52);
    List<OfferDto> list = dataProvider.getList();
    list.addAll(offers);
    this.add(cellList);
    this.setWidth("300px");
  }

}
